package com.myrent.exception;


import com.myrent.common.ReturnData;
import com.myrent.enums.CodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;
import java.util.Arrays;

@RestControllerAdvice
@Slf4j
public class ExceptionResolver {
    /**
     * 捕获自定义业务异常
     * ExceptionHandler注解用来指定具体的异常，并在方法中处理该异常信息，最后将结果通过统一的 json 结构体返回给调用者
     */
    @ExceptionHandler(value = BaseException.class)
    public ReturnData baseExceptionHandler(BaseException e) {
        return ReturnData.fail(e.getCode(), e.getMessage());
    }


    /**
     * 参数异常
     * ConstraintViolationException
     * BindException 端口被占用
     * ServletRequestBindingException
     * MethodArgumentNotValidException
     *
     * @param e 捕获的异常
     */

    @ExceptionHandler(value = {ConstraintViolationException.class, BindException.class, ServletRequestBindingException.class, MethodArgumentNotValidException.class})
    public ReturnData ConstraintViolationExceptionHandler(Exception e) {
        log.error("-------------->参数异常 {}", e.getMessage());
        return ReturnData.fail(CodeEnum.REQUEST_FAILED.getCode(), "参数异常"+e.getMessage());
    }

    @ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class})
    public ReturnData MethodNotSupportedExceptionHandler(Exception e) {
        log.error("400------------> 请求方式不被允许{}",e.getMessage());
        return ReturnData.fail(CodeEnum.REQUEST_FAILED.getCode(), "请求方式不被允许"+e.getMessage());
    }

    @ExceptionHandler(value = {NoHandlerFoundException.class})
    public ReturnData NoHandlerFoundExceptionExceptionHandler(Exception e) {
        log.error("404--------------------->访问的资源不存在，请检查访问路径{}",e.getMessage());
        return ReturnData.fail(CodeEnum.URI_NOT_EXIST.getCode(), "访问的资源不存在，请检查访问路径"+ e.getMessage());
    }


    /**
     * 拦截空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ReturnData HandleTypeMismatchException(NullPointerException ex) {
        log.error("空指针异常，{}", ex.getMessage());
        return ReturnData.fail(500, "空指针异常了"+ ex.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public ReturnData ExceptionHandler(Exception e) {
        e.printStackTrace();
        log.error("------------->Exception未捕获异常: {}", e.getMessage());
        return ReturnData.fail(CodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }

}
